<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<title>站点监控</title>
<link rel='stylesheet' href='<%- rootUrl %>/res/adapter.css' />
<link rel='stylesheet' href='<%- rootUrl %>/node_modules/font-awesome/css/font-awesome.min.css' />
<style>
    table {font-size:9px}
    td, th {padding: 3px}
    .fa-angle-double-up, .fa-angle-double-down { color: blue; padding: 3px }
</style>
<script src="<%- rootUrl %>/node_modules/jquery/dist/jquery.min.js"></script>
<script src="<%- rootUrl %>/res/adapter.js"></script>
<script src="<%- rootUrl %>/node_modules/socket.io-client/dist/socket.io.js"></script>
<script src="<%- rootUrl %>/res/manageEverything.js"></script>
<script>
function changeFrameHeight(){
    let ifm= document.getElementById("manageframe");
    ifm.height=document.documentElement.clientHeight-ifm.offsetTop-15;
}
window.onresize=function(){ changeFrameHeight();};

window.warnings = [];
window.limitTimeSpan = 30 * 60 * 1000;

function handleNewWarning(o) {
    let record = {
        t : o.ses.activetime,
        ses_active : o.ses.active.toFixed(),
        ip_active : o.ipspec.active.toFixed(),
        ses_id:o.ses._id,
        ip:o.ses.ip,
        user_id:('user' in o.ses ? o.ses.user._id : ''),
        user_nickname:('user' in o.ses ? o.ses.user.nickname : '')
    };
    let idx = window.warnings.findIndex((w) => w.ip === record.ip && w.ses_id === record.ses_id);
    if (idx > -1)
        window.warnings[idx] = record;
    else
        window.warnings.unshift(record);

    let newWarnings = [];
    window.warnings.forEach((warn) => {
        if (warn.ip === record.ip)
            warn.ip_active = record.ip_active;
        if (warn.ses_id === record.ses_id)
            warn.ses_active = record.ses_active;
        if ((warn.ip_active > 100 || warn.ses_active > 100)
            && (record.t - warn.t < window.limitTimeSpan))
            newWarnings.push(warn);
    });
    window.warnings = newWarnings;
    refreshTable();
}

function refreshTable(){
    let ifm = $('#manageframe');
    let table = $('#warnings');
    table.find('tr:gt(0)').remove();
    //table.append('<tr><th>t</th><th>ses_active</th><th>ip_active</th><th>ses_id</th><th>ip</th><th>u_id</th><th>u_nickname</th></tr>');
    for (let warn of window.warnings){
        let timetxt = (new Date(warn.t-new Date().getTimezoneOffset() * 60000).toISOString()).replace(/Z/g, '').replace(/[-:]/g, '').replace(/[T\.]/g, '-')
        table.append('<tr><td>'+timetxt+'</td><td>'+warn.ses_active+'<i class="fa fa-angle-double-up"></i><i class="fa fa-angle-double-down"></i></td><td>'+warn.ip_active+'<i class="fa fa-angle-double-up"></i><i class="fa fa-angle-double-down"></i><i></i><i></i></td>'
            +'<td>'+warn.ses_id+'</td><td><a>'+warn.ip+'</a></td><td><a>'+warn.user_id+'</a></td><td><a>'+warn.user_nickname+'</a></td></tr>');
    }
    table.find('a').attr('href', 'javascript:;');
    table.find('tr:gt(0)').each(function(trid){
        let tr = $(this);
        let user_id = '';
        tr.find('a').each(function(aid){
            let a = $(this);
            if (aid === 0){
                a.click(function(){
                    ifm.attr('src', './manageEverything?col=behavior&filter={"ip":"'+a.text()+'"}&sort={"t":-1}');
                });
            } else if (aid === 1){
                user_id = a.text();
                a.click(function(){
                    ifm.attr('src', './manageEverything?col=behavior&filter={"user_id":"'+a.text()+'"}&sort={"t":-1}');
                });
            } else if (aid === 2){
                a.click(function(){
                    ifm.attr('src', './manageEverything?col=user&filter={"_id":"'+user_id+'"}');
                });
            }
        });
        tr.find('.fa').each(function(faid){
            $(this).click(function(){
                let warn = window.warnings[trid];
                let params = {};
                if (faid<2){
                    params.ses_id = warn.ses_id;
                }else{
                    params.ip = warn.ip;
                }
                params.plus = (0.5 - faid % 2) * 2 * 300;
                $.post('./makeactive', params);
            });
        });
    });
    window.linkableTable(table);
    changeFrameHeight();
}

$(function(){
    changeFrameHeight();

    var socket=io.connect((window.useSSL ? "wss" : "ws") + "://"+window.location.hostname+":" + window.wsPort);
    socket.emit('signin', {user:window.user, channel:'sitemonitor'});
    socket.on('warningUser',handleNewWarning);
});
</script>
</head>
<body>
<alljs-widget><message-top><%- message %></message-top><user><%- username_uniq %></user></alljs-widget>
<div class="body-margin">
    <b style="margin: 6px;line-height: 32px;">正在监控</b><b style="margin-left:3em">(active><%- warningPoint %> 开始监控, active><%- forbidPoint %> 自动屏蔽)</b>
    <table id="warnings" border="1" cellspacing="0">
        <tr><th>t</th><th>ses_active</th><th>ip_active</th><th>ses_id</th><th>ip</th><th>u_id</th><th>u_nickname</th></tr>
    </table>
</div>
<iframe id="manageframe" src='./manageEverything?col=behavior&sort={"t":-1}' width="100%" onload="changeFrameHeight()" scrolling="auto" frameborder="0" />
</body>
</html>